/**
 * @param {number[]} nums
 * @return {number}
 */
// 方法1，计数：用map对每个元素进行计数
var majorityElement = function(nums) {
    let times = Math.floor(nums.length / 2)
    let map = new Map()
    for(let i = 0; i < nums.length; i++) {
        if(map.has(nums[i])) {
            map.set(nums[i], map.get(nums[i]) + 1)
        } else {
            map.set(nums[i], 1)
        }
    }
    for(let [key, value] of map) {
        if(value > times) return key
    }
};
// 方法2 摩尔投票
var majorityElement = function(nums) {
    let votes = 0, candidates
    for(let i = 0; i < nums.length; i++) {
        if(votes === 0) candidates = nums[i]
        if(candidates === nums[i]) votes += 1
        else votes -= 1
    }
    return candidates
}